package com.neu.controller;

import com.neu.base.Result;
import com.neu.entity.PDR;
import com.neu.service.PDRService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pdr")
@Tag(name = "PDR确定")
public class PDRController {
    @Autowired
    private PDRService pdrService;

    @PostMapping("/add")
    @Operation(summary = "添加PDR信息")
    public Result add(@RequestBody PDR pdr){

        return Result.success(pdrService.save(pdr));
    }

    @PostMapping("/get")
    @Parameter(name = "id", description = "项目ID", required = true)
    @Operation(summary = "获取PDR")
    public Result getPDR(@RequestParam String id){
        PDR pdr = pdrService.getById(id);
        if(pdr==null){
            pdr = new PDR();
            pdr.setProjectId(id);
            pdr.setDelFlag("1");
            pdr.setProgress(0);
            pdrService.save(pdr);
        }
        Map map = new HashMap<>();
        map.put("pdr_data", pdr);
//        System.out.println(pdr.toString());
        return Result.success(map);
    }

    @PostMapping("/update")
    @Operation(summary = "更新PDR")
    public Result updatePDR(@RequestBody PDR pdr){

        try{
            pdr.CalcPDR();
            pdr.setProgress(1);
        }catch (Exception e){
            pdr.setPdr(null);
            pdr.setProgress(0);
            System.out.println(pdr);
        }
        Boolean sign = pdrService.updateById(pdr);
        if(!sign){
            sign = pdrService.save(pdr);
        }

        return Result.success(sign);
    }

    @PostMapping("/test")
    @Operation(summary = "测试")
    public Result test(){

        return Result.success();
    }

}
